//
//  WBComposePictureView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/17.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

class WBComposePictureView: UICollectionView {
    
    // MARK: - 闭包属性 告诉外面的控制器
    var closure:(() -> ())?
    
    // MARK: - 提供一个属性 供外界赋值
    var images:[UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    // MARK: - 提供一个方法 供外界赋值
    func addImage(image:UIImage) {
        images.append(image)
        
        //显示当前控件
        self.hidden = false
        //刷新
        reloadData()
    }
    
    func setupUI(){
        //隐藏当前控件
        self.hidden = true
        backgroundColor = UIColor.init(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        //注册cell
        self.registerClass(WBComposePictureViewCell.self, forCellWithReuseIdentifier: "cell")
        //设置代理
        dataSource = self
        delegate = self
        
        let margin:CGFloat = 5
        let cellWH:CGFloat = (ScreenW - 20 - 2 * margin)/3
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(cellWH, cellWH)
        
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension WBComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if images.count == 0 || images.count == 9{
            
            return images.count
            
        }
        
        return images.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! WBComposePictureViewCell
        
        if images.count == indexPath.item{
            //说明除了0 9的情况的最后一张
            cell.image = nil
        }else{
            
            //这是有0，9的情况
            cell.image = images[indexPath.item]
            
            cell.closure = {[weak self] in
                //删除图片
                self?.images.removeAtIndex(indexPath.item)
                
                //如果数组中没有图片 就隐藏当前控件
                if self?.images.count == 0 {
                
                    self?.hidden = true
                }
                
                self?.reloadData()
                
            }
        }
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        if images.count == indexPath.item{
            
            print("点击cell")
            //告诉外界弹控制器
            
            closure?()
            
        }
    }
    
    
}

// MARK: - 自定义cell
class WBComposePictureViewCell:UICollectionViewCell{
    
    //定义闭包 告诉外界删除按钮的点击
    var closure:(()->())?
    
    // MARK: - 给外界提供接口 赋值
    var image:UIImage?{
        
        didSet{
            if image == nil{
                
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                deleteBtn.hidden = true
                
            }else{
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                
                deleteBtn.hidden = false
            }
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    func setupUI() {
        
        backgroundColor = UIColor.init(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
        
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteBtn)
        
        //设置约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        deleteBtn.snp_makeConstraints { (make) in
            make.top.trailing.equalTo(contentView)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载ImageView
    private lazy var bgImageView:UIImageView = UIImageView()
    
    // MARK: - 懒加载删除按钮
    private lazy var deleteBtn: UIButton = {
        
        let btn = UIButton()
        
        //添加按钮点击事件
        btn.addTarget(self, action: #selector(WBComposePictureViewCell.deletebtnClick), forControlEvents: .TouchUpInside)
        
        //设置按钮图片
        btn.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        return btn
    }()
    
    // MARK: - 删除按钮事件的实现
    func deletebtnClick() {
        
        closure?()
        
    }
    
}




